/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.daveychu.lingo.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.daveychu.lingo.model.Letter;
import com.daveychu.lingo.model.Lingo;

/**
 * 
 * @author Davey
 */
@SuppressWarnings("serial")
public class LingoApp extends javax.swing.JFrame {

	private Lingo lingo;
	private List<WordPanel> wordPanels = new ArrayList<WordPanel>(Lingo.NR_OF_TURNS);

	/**
	 * Creates new form LingoApp
	 */
	public LingoApp() {
		lingo = new Lingo();
		initComponents();
		initWordPanels();
		addListeners();
		startNewGame();
	}

	private void initWordPanels() {
		if (wordPanels.isEmpty()) {
			for (int i = 0; i < Lingo.NR_OF_TURNS; i++) {
				WordPanel wordPanel = new WordPanel();
				wordPanel.setBounds(60, i * 55 - 5, 300, 55);
				wordPanels.add(wordPanel);
				gamePanel.add(wordPanel);
			}
		} else {
			for (int i = 0; i < wordPanels.size(); i++) {
				wordPanels.get(i).resetLetters();
			}
		}
	}

	/**
	 * Add the listeners to the components
	 */
	private final void addListeners() {
		btnNew.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				startNewGame();
			}
		});

		btnQuit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});

		tfGuess.addKeyListener(new KeyAdapter() {

			public void keyReleased(KeyEvent arg0) {
				if (arg0.getKeyCode() == 10 && !lingo.isTurnOver()
						&& !lingo.isWon()) {
					// Check if the word is 5 letters long otherwise don't do
					// anything
					if (tfGuess.getText().length() == Lingo.NR_OF_LETTERS) {
						Letter[] letters = lingo.attemptWord(tfGuess.getText());
						int turns = lingo.getTurns();
						wordPanels.get(turns - 1).setLetters(letters);
						lingo.setTurns(turns + 1);

						if (lingo.isTurnOver() && !lingo.isWon()) {
							JOptionPane.showMessageDialog(
									LingoApp.this,
									"The correct word was: "
											+ lingo.getSolution());
						}

						lblTurn.setText(String.valueOf(lingo.getTurns()));
						lblScore.setText(String.valueOf(lingo.getScore()));
						gamePanel.repaint();
					}
				} else {
					if (tfGuess.getText().length() < Lingo.NR_OF_LETTERS) {
						lblLetterHints.setText(lingo.getLetterHints(tfGuess
								.getText()));
					} else {
						lblLetterHints.setText("[]");
					}
				}
			}

		});
	}

	protected final void startNewGame() {
		lingo.startNewGame();
		initWordPanels();
		gamePanel.repaint();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		gamePanel = new javax.swing.JPanel();
		jLabel7 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		jLabel10 = new javax.swing.JLabel();
		jLabel11 = new javax.swing.JLabel();
		jLabel12 = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		lblScore = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		lblTurn = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		tfGuess = new javax.swing.JTextField();
		btnNew = new javax.swing.JButton();
		btnQuit = new javax.swing.JButton();
		lblLetterHints = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		gamePanel.setBackground(new java.awt.Color(255, 255, 255));

		jLabel7.setText("400");

		jLabel8.setText("250");

		jLabel9.setText("200");

		jLabel10.setText("150");

		jLabel11.setText("100");

		jLabel12.setText("50");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				gamePanel);
		gamePanel.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(jLabel10)
														.addComponent(jLabel12)
														.addGroup(
																jPanel1Layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																		.addGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				jPanel1Layout
																						.createSequentialGroup()
																						.addContainerGap()
																						.addComponent(
																								jLabel11))
																		.addGroup(
																				jPanel1Layout
																						.createSequentialGroup()
																						.addGap(24,
																								24,
																								24)
																						.addGroup(
																								jPanel1Layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																										.addComponent(
																												jLabel9)
																										.addComponent(
																												jLabel8)
																										.addComponent(
																												jLabel7)))))
										.addContainerGap(348, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(34, 34, 34)
										.addComponent(jLabel7)
										.addGap(35, 35, 35)
										.addComponent(jLabel8)
										.addGap(40, 40, 40)
										.addComponent(jLabel9)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												40, Short.MAX_VALUE)
										.addComponent(jLabel10)
										.addGap(38, 38, 38)
										.addComponent(jLabel11)
										.addGap(33, 33, 33)
										.addComponent(jLabel12)
										.addGap(26, 26, 26)));

		jLabel1.setText("SCORE");

		lblScore.setText("0");

		jLabel3.setText("TURN");

		lblTurn.setText("1");

		jLabel5.setText("GUESS");

		jLabel6.setText("Possible Next Letters: ");

		btnNew.setText("NEW");

		btnQuit.setText("QUIT");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		gamePanel,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(31, 31,
																		31)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jLabel1)
																				.addComponent(
																						jLabel5)
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																								.addComponent(
																										btnNew,
																										javax.swing.GroupLayout.Alignment.LEADING,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										btnQuit,
																										javax.swing.GroupLayout.Alignment.LEADING,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE))
																				.addComponent(
																						jLabel3)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(10,
																										10,
																										10)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														lblScore,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														27,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														lblTurn,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														27,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														tfGuess,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														89,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel6)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		lblLetterHints,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		400,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		lblScore)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		jLabel3)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		lblTurn)
																.addGap(21, 21,
																		21)
																.addComponent(
																		jLabel5)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		tfGuess,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(56, 56,
																		56)
																.addComponent(
																		btnNew)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		btnQuit))
												.addComponent(
														gamePanel,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										75, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel6)
												.addComponent(lblLetterHints))
								.addContainerGap()));

		pack();
	}// </editor-fold>

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(LingoApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(LingoApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(LingoApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(LingoApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new LingoApp().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton btnNew;
	private javax.swing.JButton btnQuit;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel11;
	private javax.swing.JLabel jLabel12;
	private javax.swing.JLabel lblLetterHints;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel gamePanel;
	private javax.swing.JLabel lblScore;
	private javax.swing.JLabel lblTurn;
	private javax.swing.JTextField tfGuess;
	// End of variables declaration
}
